package cn.admobiletop.adsuyi.adapter.toutiao.data;

import android.app.Activity;

import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdDislike;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;

import cn.admobiletop.adsuyi.ad.listener.ADSuyiAdListener;
import cn.admobiletop.adsuyi.adapter.toutiao.listener.CustomDownloadListener;

/**
 * @author ciba
 * @description 描述
 * @date 2020/4/17
 */
public class TTNativeExpressAdInfo<T extends ADSuyiAdListener> extends BaseAdInfo<T, TTNativeExpressAd> {
    private Activity activity;

    public TTNativeExpressAdInfo(Activity activity, String platformPosId) {
        super(platformPosId);
        this.activity = activity;
    }

    @Override
    public void setAdapterAdInfo(TTNativeExpressAd ttNativeExpressAd) {
        super.setAdapterAdInfo(ttNativeExpressAd);
        // 如果是下载类型，设置下载监听
        setDownloadListener();

        // 设置关闭按钮触发的弹窗
        setDislikeCallback(activity);
    }

    @Override
    public void releaseAdapter() {
        super.releaseAdapter();
        activity = null;
        if (getAdapterAdInfo() != null) {
            getAdapterAdInfo().destroy();
            setAdapterAdInfo(null);
        }
    }

    /**
     * 设置下载监听
     */
    private void setDownloadListener() {
        if (getAdapterAdInfo() != null && TTAdConstant.INTERACTION_TYPE_DOWNLOAD == getAdapterAdInfo().getInteractionType()) {
            getAdapterAdInfo().setDownloadListener(new CustomDownloadListener());
        }
    }

    /**
     * 绑定不关闭广告原因弹框
     */
    private void setDislikeCallback(Activity activity) {
        if (activity != null && getAdapterAdInfo() != null) {
            getAdapterAdInfo().setDislikeCallback(activity, new TTAdDislike.DislikeInteractionCallback() {
                @Override
                public void onShow() {
                }

                @Override
                public void onSelected(int i, String s, boolean b) {
                    onCloseClick(null);
                }

                @Override
                public void onCancel() {
                }

            });
        }
    }
}
